using System; 
using Microsoft.VisualBasic;
using System.Collections;
using System.Data;
using System.Diagnostics;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using System.Globalization;




namespace NumberToTextField 
{

    public class Numeric_String : NumbertoTextBase  
	{
	
		string[] ones = new string[11];
		string[] Teens = new string[11];
		string[] Tens = new string[11];
		string[] Thousands = new string[6];
        
        //CultureInfo  locale ;

    
        private void Initilize() 
		{
            LanguageManager LANGManager = GetLanguageManager();

            ones[0] =LANGManager.GetLocalizedString("zero");
            ones[1] =LANGManager.GetLocalizedString("one");
            ones[2] =LANGManager.GetLocalizedString("two");
            ones[3] =LANGManager.GetLocalizedString("three");
            ones[4] =LANGManager.GetLocalizedString("four");
            ones[5] =LANGManager.GetLocalizedString("five");
            ones[6] =LANGManager.GetLocalizedString("six");
            ones[7] =LANGManager.GetLocalizedString("seven");
            ones[8] =LANGManager.GetLocalizedString("eight");
            ones[9] =LANGManager.GetLocalizedString("nine");
            Teens[0] =LANGManager.GetLocalizedString("ten");
            Teens[1] =LANGManager.GetLocalizedString("eleven");
            Teens[2] =LANGManager.GetLocalizedString("twelve");
            Teens[3] =LANGManager.GetLocalizedString("thirteen");
            Teens[4] =LANGManager.GetLocalizedString("fourteen");
            Teens[5] =LANGManager.GetLocalizedString("fifteen");
            Teens[6] =LANGManager.GetLocalizedString("sixteen");
            Teens[7] =LANGManager.GetLocalizedString("seventeen");
            Teens[8] =LANGManager.GetLocalizedString("eighteen");
            Teens[9] =LANGManager.GetLocalizedString("nineteen");
            Tens[0] =LANGManager.GetLocalizedString("");
            Tens[1] =LANGManager.GetLocalizedString("ten");
            Tens[2] =LANGManager.GetLocalizedString("twenty");
            Tens[3] =LANGManager.GetLocalizedString("thirty");
            Tens[4] =LANGManager.GetLocalizedString("forty");
            Tens[5] =LANGManager.GetLocalizedString("fifty");
            Tens[6] =LANGManager.GetLocalizedString("sixty");
            Tens[7] =LANGManager.GetLocalizedString("seventy");
            Tens[8] =LANGManager.GetLocalizedString("eighty");
            Tens[9] =LANGManager.GetLocalizedString("ninety");
            Thousands[0] ="";
            Thousands[1] = LANGManager.GetLocalizedString("thousand");
            Thousands[2] =LANGManager.GetLocalizedString("million");
            Thousands[3] =LANGManager.GetLocalizedString("billion");
            Thousands[4] =LANGManager.GetLocalizedString("trillion");

            //ones[0] = "zero";
            //ones[1] = "one";
            //ones[2] = "two";
            //ones[3] = "three";
            //ones[4] = "four";
            //ones[5] = "five";
            //ones[6] = "six";
            //ones[7] = "seven";
            //ones[8] = "eight";
            //ones[9] = "nine";
            //Teens[0] = "ten";
            //Teens[1] = "eleven";
            //Teens[2] = "twelve";
            //Teens[3] = "thirteen";
            //Teens[4] = "fourteen";
            //Teens[5] = "fifteen";
            //Teens[6] = "sixteen";
            //Teens[7] = "seventeen";
            //Teens[8] = "eighteen";
            //Teens[9] = "nineteen";
            //Tens[0] = "";
            //Tens[1] = "ten";
            //Tens[2] = "twenty";
            //Tens[3] = "thirty";
            //Tens[4] = "forty";
            //Tens[5] = "fifty";
            //Tens[6] = "sixty";
            //Tens[7] = "seventy";
            //Tens[8] = "eighty";
            //Tens[9] = "ninety";
            //Thousands[0] = "";
            //Thousands[1] = "thousand"; 
            //Thousands[2] = "million";
            //Thousands[3] = "billion";
            //Thousands[4] = "trillion";

              
			
			
		}
        //LanguageManager GetLanguageManager()
        //{
        //    LanguageManager LANGManager;
        //    if (locale == null)
        //        LANGManager = new LanguageManager(System.Threading.Thread.CurrentThread.CurrentUICulture);
        //    else
        //        LANGManager = new LanguageManager(locale);

        //    return LANGManager;
        //}
        public override string parse(string Value)
        {
            return EnglishNumToText(long.Parse(Value));
        }
		public string EnglishNumToText(long Number) 
		{
            LanguageManager LANGManager = GetLanguageManager();

            Initilize();
			int nCol = 0, nChar = 0;
			bool bAllZeros = true; 
			bool bShowThousands = false;
			string strBuff = null, strTemp = null;
			string strVal = Number.ToString();

            if (Microsoft.VisualBasic.Strings.Len(strVal) > 15)
			{
				strBuff = "#Error#";
				return strBuff;
			}
			else
			{
				// Trap errors
				try
				{
					// Iterate through string
                    for (int i = Microsoft.VisualBasic.Strings.Len(strVal); i >= 1; i += -1)
					{
						// Get value of this digit
                        nChar = System.Convert.ToInt32(Conversion.Val(Microsoft.VisualBasic.Strings.Mid(strVal, i, 1)));
						// Get column position
                        nCol = (Microsoft.VisualBasic.Strings.Len(strVal) - i) + 1;
						// Action depends on 1's, 10's or 100's column
						switch ((nCol % 3))
						{
							case 1:
								bShowThousands = true;
								if (i == 1)
								{
									// First digit in number (last in the loop)
									strTemp = ones[nChar] + " ";
								}
                                else if (Microsoft.VisualBasic.Strings.Mid(strVal, i - 1, 1) == "1")
								{
									// This digit is part of "teen" number
									strTemp = Teens[nChar] + " ";
									i = i - 1; // Skip tens position
								} 
								else if (nChar > 0)
								{
									// Any non-zero digit
									strTemp = ones[nChar] + " ";
								}
								else
								{
									// This digit is zero. If digit in tens and hundreds column
									// are also zero, don't show "thousands"
									bShowThousands = false;
									// Test for non-zero digit in this grouping
                                    if (Microsoft.VisualBasic.Strings.Mid(strVal, i - 1, 1) != "0")
									{
										bShowThousands = true;
									}
									else if (i > 2)
									{
                                        if (Microsoft.VisualBasic.Strings.Mid(strVal, i - 2, 1) != "0")
										{
											bShowThousands = true;
										}
									}
									strTemp = "";
								}
								// Show "thousands" if non-zero in grouping
								if (bShowThousands)
								{
									if (nCol > 1)
									{
										strTemp = strTemp + Thousands[nCol / 3];
										if (bAllZeros)
										{
											strTemp = strTemp + " ";
										}
										else
										{
											strTemp = strTemp + ", ";
										}
									}
									// Indicate non-zero digit encountered
									bAllZeros = false;
								}
								strBuff = strTemp + strBuff;
								break;
							case 2:
								if (nChar > 0)
								{
                                    if (Microsoft.VisualBasic.Strings.Mid(strVal, i + 1, 1) != "0")
									{
										strBuff = Tens[nChar] + "-" + strBuff;
									}
									else
									{
										strBuff = Tens[nChar] + " " + strBuff;
									}
								}
								break;
							case 0:
								if (nChar > 0)
								{
									strBuff = ones[nChar] + LANGManager.GetLocalizedString("hundred") + strBuff;
								}
								break;
						}
					} 
					// Convert first letter to upper case
                    strBuff = Microsoft.VisualBasic.Strings.UCase(Microsoft.VisualBasic.Strings.Left(strBuff, 1)) + Microsoft.VisualBasic.Strings.Mid(strBuff, 2);
					// Return result
                    return strBuff.TrimEnd();
                    //return System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
                    
				}
				catch
				{
				
					return "Error Number";
				}
			}
		}
        //void INumbertoText.setLocale(CultureInfo  Value)
        //{
        //    locale = Value;
        //}
        


      



		

	}
}